<template>
  <div class="dialog-wrapper" v-if="visible">
    <div class="dialog-mask"></div>
    <div class="dialog-container">
      <!-- 对话框内容 -->
      <div class="dialog-content">
        <h3>
          <font-awesome-icon :icon="['fas', 'house-lock']" style="margin-right: 8px; color: #FFA500;" />
          {{room.name}}
        </h3>
        <div class="dialog-body">
          <div class="password-input-container">
            <el-input
                placeholder="请输入房间密码"
                class="dialog-input"
                v-model="password"
                show-password
                @input="validatePassword"
            ></el-input>
          </div>
          <!-- 错误提示信息 -->
          <p class="error-message" v-if="!isPasswordValid && password.length > 0">
            {{ password.length < 6 ? '密码长度不能少于6位' : '' }}
          </p>
        </div>
        <div class="dialog-footer">
          <button class="cancel-btn" @click="close">取消</button>
          <button
              class="confirm-btn"
              @click="confirm"
              :disabled="!isPasswordValid"
              :class="{ 'disabled-btn': !isPasswordValid }">
            确认
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "RoomPassDialog",
  props: {
    confirmRoomPass: {
      type: Function
    }
  },
  data() {
    return {
      visible: false,
      password: '',
      room: {
        name: '测试房间'
      },
      isPasswordValid: false // 密码是否有效的标志
    }
  },
  methods: {
    show(room) {
      this.room = room;
      this.visible = true;
      this.password = ''; // 重置密码
      this.validatePassword(); // 初始化校验状态
    },
    close() {
      this.visible = false;
    },
    async confirm() {
      let result = await this.confirmRoomPass(this.room, this.password);
      if (result){
        this.close();
      }
    },
    // 密码校验方法
    validatePassword() {
      // 密码不为空且长度大于等于6位才有效
      this.isPasswordValid = this.password.length >= 6;
    }
  },
  // 初始化时进行一次校验
  mounted() {
    this.validatePassword();
    if (this.$el && this.$el.parentNode !== document.body) {
      document.body.appendChild(this.$el)
    }
  },
  beforeDestroy() {
    if (this.$el && this.$el.parentNode === document.body) {
      document.body.removeChild(this.$el)
    }
  }
}
</script>

<style scoped>
.dialog-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  overflow: hidden;
}

.dialog-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
}

.dialog-container {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.dialog-content {
  width: 450px;
  max-width: 90vw;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  transform: translateZ(0);
  position: relative;
  background: rgba(30, 30, 45, 0.95);
  border-radius: 12px;
  backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

h3 {
  margin: 0;
  padding: 20px 24px;
  color: #fff;
  font-size: 1.1rem;
  font-weight: 600;
  background: rgba(255, 165, 0, 0.1);
  border-bottom: 1px solid rgba(255, 165, 0, 0.2);
  display: flex;
  align-items: center;
}

.dialog-body {
  padding: 24px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.password-input-container {
  width: 100%;
  margin-bottom: 8px;
}

/* 自定义el-input样式 */
.dialog-input {
  width: 100%;
}

/* 使用深度选择器覆盖Element UI默认样式 */
::v-deep .dialog-input .el-input__inner {
  box-sizing: border-box;
  padding: 0.5rem 0.75rem;
  border: 1px solid rgba(255, 165, 0, 0.3);
  border-radius: 8px;
  font-size: 0.95rem;
  background-color: rgba(30, 30, 45, 0.8);
  color: #fff;
  outline: none;
  height: 42px;
  transition: all 0.3s ease;
}

/* 聚焦状态样式 */
::v-deep .dialog-input .el-input__inner:focus {
  border-color: #FFA500;
  box-shadow: 0 0 0 3px rgba(255, 165, 0, 0.15);
  background-color: rgba(40, 40, 60, 0.8);
}

/* 密码切换按钮样式 */
::v-deep .dialog-input .el-input__suffix {
  color: rgba(255, 255, 255, 0.6);
}

::v-deep .dialog-input .el-input__icon {
  color: rgba(255, 255, 255, 0.6);
  transition: color 0.2s;
}

::v-deep .dialog-input .el-input__icon:hover {
  color: #FFA500;
}

/* 错误提示样式 */
.error-message {
  color: #ff4d4f;
  font-size: 0.85rem;
  margin: 8px 0 0 0;
  align-self: flex-start;
  display: flex;
  align-items: center;
  gap: 4px;
}

.dialog-footer {
  padding: 16px 24px;
  border-top: 1px solid rgba(255, 255, 255, 0.05);
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

button {
  padding: 8px 20px;
  cursor: pointer;
  border: none;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 500;
  transition: all 0.3s ease;
  min-width: 80px;
  text-align: center;
}

.cancel-btn {
  background-color: rgba(255, 255, 255, 0.1);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.cancel-btn:hover {
  background-color: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.2);
}

.confirm-btn {
  background-color: #FFA500;
  color: #fff;
  border: 1px solid #FFA500;
}

.confirm-btn:hover {
  background-color: #FF8C00;
  border-color: #FF8C00;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(255, 165, 0, 0.3);
}

/* 禁用状态的按钮样式 */
button.disabled-btn {
  opacity: 0.6;
  cursor: not-allowed;
  background-color: rgba(255, 255, 255, 0.05) !important;
  border-color: rgba(255, 255, 255, 0.05) !important;
  transform: none !important;
  box-shadow: none !important;
}

button.disabled-btn:hover {
  background-color: rgba(255, 255, 255, 0.05) !important;
  border-color: rgba(255, 255, 255, 0.05) !important;
}
</style>